import 'package:enjoy_home/pages/home/detail.dart';
import 'package:enjoy_home/pages/login/login_page.dart';
import 'package:enjoy_home/pages/mine/profile.dart';
import 'package:enjoy_home/pages/tab-bar/tab_bar_page.dart';
import 'package:enjoy_home/utils/token_manager.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      routes: {
        '/': (context) => const TabBarPage(),
        '/detail': (context) => const Detail(),
      },
      // 获取用户跳转页面路径，
      // 如果有token，则直接放行
      onGenerateRoute: (settings) {
        print(settings);
        final TokenManager manager = TokenManager();
        final token = manager.getToken() ?? '';
        // 如果没有token，而且去的页面不是登录页跳转登录页
        if (token.isEmpty && settings.name != '/login') {
          return MaterialPageRoute(builder: (context) => const LoginPage());
        }
        // 去编辑个人信息页面
        if (settings.name == 'profile') {
          return MaterialPageRoute(builder: (context) => const Profile());
        }
        // 404页面
        return null;
      },
    ),
  );
}
